global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_MFvictim_data_clean_new", clear
 
local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummy used in the event studies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

replace min_yr_mf = 0 if missing(min_yr_mf)
bys match_id : egen mf_year = max(min_yr_mf)


*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_1 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

capture program drop eventStudyGraphs_victim
program define eventStudyGraphs_victim
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies  if year_start_cohab >= mf_year, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
	reghdfe `a'  treatPost,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\MF_match_`a'.pdf", replace
	restore
	}
end


eventStudyGraphs_victim "ptoim1" "-0.04(0.02)0.04" "Employment" 


eventStudyGraphs_victim "tyotu" "-1500(500)1000" "Earnings" 



